import { defineStore } from "pinia";
import { ref } from "vue";
import { loginApi } from "../../api/user";

// export const useUserStore = defineStore("user", {
//   state: () => {
//     return {};
//   },
//   getters: {},
//   actions: {},
// });

/*
  pinia 和 vuex 区别：
    1. pinia没有mutation
    2. 定义模块不需要汇总
    3. pinia typescript支持更友好
*/

export const useUserStore = defineStore("user", () => {
  // state
  const token = ref(uni.getStorageSync("token") || "");

  // actions
  const login = async (code: string) => {
    const res = await loginApi(code);
    token.value = res.token;
    uni.setStorageSync("token", res.token);
  };

  const logout = async () => {
    token.value = "";
    uni.removeStorageSync("token");
  };

  return {
    token,
    login,
    logout,
  };
});
